package com.lawyer.util.log;

import com.github.pagehelper.StringUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description: LoggerUtil
 * @author: wzq
 * @since: 2018/07/18
 */
public class LoggerUtil {

    final static String FQCN = LoggerUtil.class.getCanonicalName();

    /**
     * key分隔"
     */
    public static final String KEY_SEPARATOR = ",";

    /**
     * 默认key前缀
     */
    public static final String DEFAULT_KEY_PREFIX = "default_key_";

    /**
     * subject key
     */
    public static final String SUBJECT_KEY = "subject";

    /**
     * 返回json日志消息
     * @param jsonObject json对象
     * @return
     */
    public static String getLoggerMessage(JSONObject jsonObject) {
        if(jsonObject == null){
            return "";
        }
        return jsonObject.toString();
    }

    /**
     * 返回json日志消息
     * @param keys key列表, 多个用"号",”隔"
     * @param values value列表
     * @return
     */
    public static String getLoggerMessage(String keys, Object... values) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        if(StringUtil.isNotEmpty(keys)) {
            String[] keyArray = keys.replaceAll("\\s*", "").split(KEY_SEPARATOR);
            for(int i = 0; i < keyArray.length && i < values.length; ++i) {
                map.put(keyArray[i], values[i] == null ? "" : values[i]);
            }
        }
        return getLoggerMessage(map);
    }

    /**
     * 返回json日志消息
     * @param values value列表
     * @return
     */
    public static String getLoggerMessage(Object... values) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        for(int i = 0; i < values.length; ++i) {
            map.put(DEFAULT_KEY_PREFIX + "_" + i, values[i] == null ? "" : values[i]);
        }
        return getLoggerMessage(map);
    }

    /**
     * 返回json日志消息
     * @param object object
     * @return
     */
    public static String getLoggerMessage(Object object) {
        return getLoggerMessage(JSONObject.fromObject(object));
    }

    /**
     * 返回json日志消息
     * @param subject 主题
     * @return
     */
    public static String getLoggerMessage(String subject) {
        return getLoggerMessage(SUBJECT_KEY, subject);
    }

    /**
     * 记录info日志
     * @param logger
     * @param jsonObject
     */
    public static void logInfoMessage(Logger logger, JSONObject jsonObject) {
        logger.log(Level.INFO, getLoggerMessage(jsonObject));
    }

    /**
     * 记录info日志
     * @param logger
     * @param keys
     * @param values
     */
    public static void logInfoMessage(Logger logger, String keys, Object... values) {
        logger.log(Level.INFO, getLoggerMessage(keys, values) );
    }

    /**
     * 记录info日志
     * @param logger
     * @param values
     */
    public static void logInfoMessage(Logger logger, Object... values) {
        logger.log(Level.INFO, getLoggerMessage(values) );
    }

    /**
     * 记录info日志
     * @param logger
     * @param object
     */
    public static void logInfoMessage(Logger logger, Object object) {
        logger.log(Level.INFO, getLoggerMessage(object) );
    }

    /**
     * 记录info日志
     * @param logger
     * @param subject
     */
    public static void logInfoMessage(Logger logger, String subject) {
        logger.log(Level.INFO, getLoggerMessage(subject) );
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param jsonObject
     */
    public static void logErrorMessage(Logger logger, Throwable t, JSONObject jsonObject) {
        logger.log(Level.ERROR, getLoggerMessage(jsonObject), t);
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param keys
     * @param values
     */
    public static void logErrorMessage(Logger logger,Throwable t, String keys, Object... values) {
        logger.log(Level.ERROR, getLoggerMessage(keys, values), t);
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param values
     */
    public static void logErrorMessage(Logger logger, Throwable t, Object... values) {
        logger.log(Level.ERROR, getLoggerMessage(values), t);
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param object
     */
    public static void logErrorMessage(Logger logger, Throwable t, Object object) {
        logger.log(Level.ERROR, getLoggerMessage(object), t);
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param subject
     */
    public static void logErrorMessage(Logger logger, Throwable t, String subject) {
        logger.log(Level.ERROR, getLoggerMessage(subject), t);
    }
    /**
     * 记录info日志
     * @param logger
     * @param jsonObject
     */
    public static void info(Logger logger, JSONObject jsonObject) {
        logger.log(Level.INFO, getLoggerMessage(jsonObject));
    }

    /**
     * 记录info日志
     * @param logger
     * @param keys
     * @param values
     */
    public static void info(Logger logger, String keys, Object... values) {
        logger.log(Level.INFO, getLoggerMessage(keys, values));
    }

    /**
     * 记录info日志
     * @param logger
     * @param values
     */
    public static void info(Logger logger, Object... values) {
        logger.log(Level.INFO, getLoggerMessage(values));
    }

    /**
     * 记录info日志
     * @param logger
     * @param object
     */
    public static void info(Logger logger, Object object) {
        logger.log(Level.INFO, getLoggerMessage(object));
    }

    /**
     * 记录info日志
     * @param logger
     * @param subject
     */
    public static void info(Logger logger, String subject) {
        logger.log(Level.INFO, getLoggerMessage(subject));
    }
    /**
     * 记录warn日志
     * @param logger
     * @param jsonObject
     */
    public static void warn(Logger logger, JSONObject jsonObject) {
        logger.log(Level.WARN, getLoggerMessage(jsonObject));
    }

    /**
     * 记录warn日志
     * @param logger
     * @param keys
     * @param values
     */
    public static void warn(Logger logger, String keys, Object... values) {
        logger.log(Level.WARN, getLoggerMessage(keys, values));
    }

    /**
     * 记录warn日志
     * @param logger
     * @param values
     */
    public static void warn(Logger logger, Object... values) {
        logger.log(Level.WARN, getLoggerMessage(values));
    }

    /**
     * 记录warn日志
     * @param logger
     * @param object
     */
    public static void warn(Logger logger, Object object) {
        logger.log(Level.WARN, getLoggerMessage(object));
    }

    /**
     * 记录warn日志
     * @param logger
     * @param subject
     */
    public static void warn(Logger logger, String subject) {
        logger.log(Level.WARN, getLoggerMessage(subject));
    }
    
    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param jsonObject
     */
    public static void error(Logger logger, Throwable t, JSONObject jsonObject) {
        logger.log(Level.ERROR, getLoggerMessage(jsonObject), t);
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param keys
     * @param values
     */
    public static void error(Logger logger,Throwable t, String keys, Object... values) {
        logger.log(Level.ERROR, getLoggerMessage(keys, values), t);
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param values
     */
    public static void error(Logger logger, Throwable t, Object... values) {
        logger.log(Level.ERROR, getLoggerMessage(values), t);
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param object
     */
    public static void error(Logger logger, Throwable t, Object object) {
        logger.log(Level.ERROR, getLoggerMessage(object), t);
    }

    /**
     * 记录error日志
     * @param logger
     * @param t
     * @param subject
     */
    public static void error(Logger logger, Throwable t, String subject) {
        logger.log(Level.ERROR, getLoggerMessage(subject), t);
    }
}
